home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / ^Php / php album / ALBUM.PHP < prev    next >
Encoding:
PHP Script  |  2001-02-18  |  2.5 KB  |  91 lines

  1. <?php
  2.     /* album.php - version 1
  3.      * .net magazine (www.netmag.co.uk), issue 83
  4.      * Matt Kynaston, 2001
  5.      * Distributed under the GNU Public License - www.gnu.org/copyleft/gpl.html
  6.      *
  7.      * This version of the PHP Photo Album displays all the images in the 'photos'
  8.      * directory. It gives the user the opportunity to upload their own photos to 
  9.      * the album.
  10.     */
  11. ?>
  12.  
  13. <html>
  14. <head>
  15. <title>Photo Album</title>
  16. </head>
  17.  
  18. <body bgcolor="#FFFFFF" text="#000000">
  19. <h1>PHP Photo Album</h1>
  20. <h3>(ugly version)</h3>
  21.  
  22. <?php
  23.     /* check to see if data has been posted to this page, and make sure it isn't blank
  24.      * (as would happen if someone clicked the 'submit' button without first selecting
  25.      * file).
  26.      *
  27.      * Note: when file is first uploaded, it's saved in a temporary directory. We must
  28.      * copy it or it will be deleted once the viewer leaves this page
  29.     */
  30.     if (isset($ulFile) && $ulFile_name) {
  31.         // copy uploaded file to photo directory
  32.         if (copy($ulFile, "photos/$ulFile_name")) {
  33.             print "<p>$ulFile_name successfully uploaded</p>\n";
  34.         } else {
  35.             // print error message if it didn't work
  36.             print "<p>Oops! Couldn't upload $ulFile_name</p>\n";
  37.         }
  38.         // delete temporary file
  39.         unlink($ulFile);
  40.     }    
  41. ?>
  42.  
  43.  
  44. <!-- the upload form                                                   -->
  45. <!-- notice how we use $PHP_SELF so uploads are sent back to this page -->
  46. <form name="form1" method="post" action="<?php print $PHP_SELF?>" enctype="multipart/form-data">
  47.   <p>Add your own image to the album:<br>
  48.     <input type="file" name="ulFile">
  49.   </p>
  50.   <p>
  51.     <input type="submit" name="Submit" value="Submit">
  52.   </p>
  53. </form>
  54.  
  55. <p> </p>
  56. <table width="100%" border="0" cellspacing="10">
  57.     <tr>
  58.     <?php
  59.         // this section generates table rows filled with images
  60.         $num_cols = 4;
  61.         $dirname = "photos";
  62.         
  63.         $col = 0;
  64.         $dh = opendir( $dirname ); // open directory for reading
  65.         
  66.         // loop through all files in photo directory, reading filename into $file
  67.         while($file=readdir($dh)) {
  68.             if ($file=="." || $file=="..") continue;// ignore directory files
  69.             
  70.             // output table cell with image inside
  71.             print "<td><img src='$dirname/$file'><br>$file</td>\n";
  72.             $col++;
  73.             
  74.             // if max number of columns reached, start new row
  75.             if ($col==$num_cols) {
  76.                 print "</tr>\n<tr>\n";
  77.                 $col=0;
  78.             }
  79.         }
  80.         
  81.         // fill rest of row with empty cells
  82.         for ($i=$col;$i<=$num_cols;$i++) {
  83.             print "<td></td>\n";
  84.         }
  85.     ?>
  86.     </tr>
  87. </table>
  88.  
  89. </body>
  90. </html>
  91.